﻿// Copyright (C) Microsoft Corporation. All Rights Reserved.
// This code released under the terms of the
// Microsoft Limited Public License (Ms-LPL).

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;

using Microsoft.VisualStudio.IntelliTrace;

namespace Microsoft.VisualStudio.Samples
{
    internal class DiagnosticChainFormatter : EventChainFormatter
    {

        IList<DiagnosticChainHandler> handlers;

        public DiagnosticChainFormatter(IntelliTraceProcess process)
            : base(process)
        {
            handlers = new List<DiagnosticChainHandler>();
        }
       
        public override void FireEvents()
        {
            if (handlers.Count > 0) base.FireEvents();
        }

		protected override void FireEvent(IntelliTraceEvent ev)
        {
            foreach(DiagnosticChainHandler handler in handlers){
                if( ev.GetType() == typeof(DiagnosticEvent) || ev.GetType().IsSubclassOf(typeof(DiagnosticEvent)) ){
                    handler.DiagnosticEvent(ev);
                } else if( ev.GetType() == typeof(DebuggerInteractionEvent) || ev.GetType().IsSubclassOf(typeof(DebuggerInteractionEvent)) ){
                    handler.UIRecordedEvent(ev);
                }
            }
        }

        protected override StreamChain getStreamChain()
        {
            return Process.CreateProcessChain<DiagnosticStreamChain>();
        }

        public void AddHandler(DiagnosticChainHandler handler)
        {
            handlers.Add(handler);
        }

    }
}
